package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.cache.util.RedisKeysUtil;
import cn.wolfcode.luowowo.cache.vo.JinPaiStatisVo;
import cn.wolfcode.luowowo.comment.domain.SheQuWenDa;
import cn.wolfcode.luowowo.comment.domain.WenTiHuiDa;
import cn.wolfcode.luowowo.comment.repository.SheQuWenDaServiceRepository;
import cn.wolfcode.luowowo.comment.repository.WenTiHuiDaServiceRepository;
import cn.wolfcode.luowowo.comment.service.ISheQuWenDaService;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;

@Service
public class SheQuWenDaServiceImpl implements ISheQuWenDaService {
    @Autowired
    private SheQuWenDaServiceRepository repository;
    @Autowired
    private WenTiHuiDaServiceRepository wenRepository;
    @Reference
    private IUserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate template;

    @Override
    public String save(SheQuWenDa sheQuWenDa) {
        if (sheQuWenDa == null) {
            throw new LogicException("参数错误");
        }
        String content = sheQuWenDa.getContent();
        if (content == null) {
            throw new LogicException("没有编写内容");
        }
        if (content.length() > 200) {
            sheQuWenDa.setSummary(content.substring(0, 200));
        } else {
            sheQuWenDa.setSummary(content);
        }
        sheQuWenDa.setCreateTime(new Date());
        SheQuWenDa save = repository.save(sheQuWenDa);
        return save.getId();
    }

    @Override
    public List<SheQuWenDa> selectWenTi(SheQuWenDa sheQuWenDa) {
        return repository.findAll();
    }

    @Override
    public SheQuWenDa selectHuiDa(SheQuWenDa sheQuWenDa) {
        return repository.findById(sheQuWenDa.getId()).get();
    }

    @Override
    public void getNum(String problemId) {
        SheQuWenDa da = repository.findById(problemId).get();
        int i = da.getThumbupnum();
        da.setThumbupnum(++i);
        repository.save(da);
    }

    @Override
    public SheQuWenDa selectNum(String problemId) {
        SheQuWenDa num = repository.findById(problemId).get();
        return num;
    }

    @Override
    public void addViewNum(String problemId) {
        SheQuWenDa da = repository.findById(problemId).get();
        int i = da.getViewnum();
        da.setViewnum(++i);
        repository.save(da);
    }

    @Override
    public void saveReply(WenTiHuiDa wenTiHuiDa, Long userId) {
        String content = wenTiHuiDa.getContent();
        if (content == null) {
            throw new LogicException("回答不能为空");
        }
        if (content.length() > 250) {
            wenTiHuiDa.setStatus(1);
            //查出之前的金牌数,然后加一
            int i = wenTiHuiDa.getUserInfo().getGoldMedal() + 1;
            userInfoService.updateGoldMedal(i, wenTiHuiDa.getUserInfo().getId());
            //查询在redis中是否存在
            String key = RedisKeysUtil.USER_JIN_PAI.getRedisKeyPre();//user_jin_pai_num:
            template.opsForZSet().incrementScore(key, RedisKeysUtil.USER_JIN_PAI.join(userId.toString()), 1);
        }
        String sheQuWenDaId = wenTiHuiDa.getSheQuWenDaId();
        SheQuWenDa sheQuWenDa = repository.findById(sheQuWenDaId).get();
        int h = sheQuWenDa.getReply();
        sheQuWenDa.setReply(++h);
        repository.save(sheQuWenDa);
        wenRepository.save(wenTiHuiDa);
    }

    @Override
    public List<WenTiHuiDa> queryWenTiHuiDa(String id) {

        return wenRepository.findBySheQuWenDaId(id);
    }
}
